package com.jrsoft.bim.util.math;

import lombok.Data;

/**
 * ${DESCRIPTION}
 *
 * @author Blueeyedboy
 * @create 2019-12-31 12:12 PM
 **/

@Data
public class LinearEquation {
	Point a, b, c, d;
	double x, y;

	public LinearEquation(Point a, Point b, Point c, Point d) {
		this.a = a;
		this.b = b;
		this.c = c;
		this.d = d;
	}

	public boolean solve() {
		double denominator = (b.y - a.y) * (d.x - c.x) - (a.x - b.x)
				* (c.y - d.y);
		if (Math.abs(denominator) < 0.000001){
			return false;
		}

		this.x = ((b.x - a.x) * (d.x - c.x) * (c.y - a.y) + (b.y - a.y)
				* (d.x - c.x) * a.x - (d.y - c.y) * (b.x - a.x) * c.x)
				/ denominator;
		this.y = -((b.y - a.y) * (d.y - c.y) * (c.x - a.x) + (b.x - a.x)
				* (d.y - c.y) * a.y - (d.x - c.x) * (b.y - a.y) * c.y)
				/ denominator;
		if ((x - a.x) * (x - b.x) <= 0 && (y - a.y) * (y - b.y) <= 0
				&& (x - c.x) * (x - d.x) <= 0 && (y - c.y) * (y - d.y) <= 0){
			return true;
		}

		return false;
	}
	public static void main(String[] args) {

			/*LinearEquation ans = new LinearEquation(p[0], p[1], p[2], p[3]);
			if (ans.solve()) {
				System.out.printf("The intersecting point is: (%.2f,%.2f)\n",
						ans.x, ans.y);
			} else{
				System.out.println("The two lines do not cross");
			}*/

	}
}


